Conditions | 11 |
Total Lines | 60 |
Code Lines | 46 |
Lines | 28 |
Ratio | 46.67 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like Upload.initDropzone often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /** |
||
35 | initDropzone() { |
||
36 | if (typeof Dropzone != 'undefined') { |
||
37 | var urlAction = $('[name="dropzone-url"]').val(); |
||
38 | |||
39 | var previewNode = $('#dropzone-preview'); |
||
40 | previewNode.removeAttr('id'); |
||
41 | var previewTemplate = previewNode.parent().html(); |
||
42 | previewNode.parent().remove(); |
||
43 | |||
44 | var mediaDropzone = $(".dropzone-form").dropzone({ |
||
45 | url: urlAction, |
||
46 | autoProcessQueue: true, |
||
47 | thumbnailWidth: null, |
||
48 | thumbnailHeight: null, |
||
49 | previewTemplate: previewTemplate, // Define the container to display the previews |
||
50 | //clickable: ".fileinput-button", // Define the element that should be used as click trigger to select files. |
||
51 | }); |
||
52 | |||
53 | View Code Duplication | mediaDropzone.on("addedfile", function (file) { |
|
|
|||
54 | var fileId = 'media' + document.querySelectorAll('.media-list-item').length; |
||
55 | file.previewElement.getElementsByTagName('input')[0].setAttribute('id', fileId); |
||
56 | file.previewElement.getElementsByTagName('label')[0].setAttribute('for', fileId); |
||
57 | |||
58 | var imagesFileTypes = ['image/png', 'image/jpg', 'image/jpeg', 'image/gif']; |
||
59 | if (imagesFileTypes.indexOf(file.type) != -1) { |
||
60 | file.previewElement.querySelector('.media-item-file-details').style.display = 'none'; |
||
61 | } else if (file.type === 'application/pdf') { |
||
62 | file.previewElement.querySelector('.media-item-file-details').style.display = 'block'; |
||
63 | file.previewElement.querySelector('.media-item-icon').innerHTML = '<i class="fas fa-file-pdf"></i>'; |
||
64 | } else if (file.type === 'application/doc' | 'application/docx') { |
||
65 | file.previewElement.querySelector('.media-item-file-details').style.display = 'block'; |
||
66 | file.previewElement.querySelector('.media-item-icon').innerHTML = '<i class="fas fa-file-word"></i>'; |
||
67 | } else if (file.type === 'application/ppt' | 'application/pptx') { |
||
68 | file.previewElement.querySelector('.media-item-file-details').style.display = 'block'; |
||
69 | file.previewElement.querySelector('.media-item-icon').innerHTML = '<i class="fas fa-file-powerpoint"></i>'; |
||
70 | } else if (file.type === 'video/mp4' | 'video/webm' | 'video/mkv') { |
||
71 | file.previewElement.querySelector('.media-item-file-details').style.display = 'block'; |
||
72 | file.previewElement.querySelector('.media-item-icon').innerHTML = '<i class="fas fa-file-video"></i>'; |
||
73 | } else if (file.type === 'audio/mpeg') { |
||
74 | file.previewElement.querySelector('.media-item-file-details').style.display = 'block'; |
||
75 | file.previewElement.querySelector('.media-item-icon').innerHTML = '<i class="fas fa-file-audio"></i>'; |
||
76 | } else { |
||
77 | file.previewElement.querySelector('.media-item-file-details').style.display = 'block'; |
||
78 | file.previewElement.querySelector('.media-item-icon').innerHTML = '<i class="fas fa-file"></i>'; |
||
79 | } |
||
80 | }); |
||
81 | |||
82 | mediaDropzone.on("success", function (file, resp) { |
||
83 | file.previewElement.querySelector(".media-list-item").classList.remove('uploading'); |
||
84 | file.previewElement.querySelector(".upload-progress").style.display = 'none'; |
||
85 | file.previewElement.querySelector(".media-item-file-extension").innerHTML = file.type; |
||
86 | }); |
||
87 | |||
88 | mediaDropzone.on("error", function (file) { |
||
89 | file.previewElement.querySelector(".media-list-item").classList.remove('uploading'); |
||
90 | file.previewElement.querySelector(".upload-progress").style.display = 'none'; |
||
91 | file.previewElement.querySelector(".media-item-file-extension").innerHTML = file.type; |
||
92 | }); |
||
93 | } |
||
94 | } |
||
95 | |||
99 | } |